<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="服务方名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入服务方名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pm:supplier:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pm:supplier:remove']"
          >删除</el-button
        >
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['pm:supplier:export']"-->
<!--          >导出</el-button-->
<!--        >-->
<!--      </el-col>-->


      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <TablePage
      :loading="loading"
      :dataList="supplierList"
      :tableHead="tableHead"
      :showSelection="true"
      :total="total"
      :pageNum="queryParams.pageNum"
      :pageSize="queryParams.pageSize"
      @handleSelectionChange="handleSelectionChange"
      @pagination="pagination"
    >
      <template slot="rowOperate" slot-scope="scope">
        <el-button
          size="mini"
          type="text"
          icon="el-icon-edit"
          @click="handleUpdate(scope.row)"
          v-hasPermi="['pm:supplier:edit']"
        >修改</el-button>
        <el-button
          size="mini"
          type="text"
          icon="el-icon-delete"
          @click="handleDelete(scope.row)"
          v-hasPermi="['pm:supplier:remove']"
        >删除</el-button>

        <el-button
          size="mini"
          type="text"
          icon="el-icon-setting"
          @click="handleAddRequestFunc(scope.row)"
        >配置请求方法</el-button>
      </template>
    </TablePage>


    <!-- 添加或修改供应商信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" class="addDialog" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="服务方名称" prop="supplierName">
          <el-input
            v-model="form.supplierName"
            placeholder="请输入服务方名称"
          />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            placeholder="请输入备注"
          />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="method.title" :visible.sync="method.open"   width="1000px"  append-to-body>
      <Method :supplierId="supplierId" />
    </el-dialog>

    <!--  配置请求方法  -->
    <el-dialog :title="requestMethod.title" :visible.sync="requestMethod.open"   width="1000px"  append-to-body>
      <RequestMethod :supplierId="supplierId" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listSupplier,
  getSupplier,
  delSupplier,
  addSupplier,
  updateSupplier,
} from "@/api/pm/supplier";


import {cleanLogininfor} from "@/api/monitor/logininfor";
import Method from './method';
import RequestMethod from "@/views/pm/supplier/requestMethod";
import TablePage from '@/components/TablePage/index';

export default {
  name: "Supplier",
  components: {Method, RequestMethod, TablePage},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商信息表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // table参数
      tableHead: [
        //{label: '', props: 'supplierId',   position: 'center', fieldType: ''},
        {label: '服务方名称',   props: 'supplierName', position: 'center', fieldType: ''},
        {label: '备注',   props: 'remark', position: 'center', fieldType: ''},
      ],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierName: null,
      },
      // 表单参数
      form: {
        supplierName: '',
        remark: '',
      },
      // 表单校验
      rules: {
        supplierName: [
          { required: true, message: "服务方名称不能为空", trigger: "blur" },
        ],
      },
      // 显示配置供应商方法
      supplierId: '',
      method: {
        title: '方法列表',
        open: false
      },
      requestMethod: {
        title: '请求方法列表',
        open: false
      }
    };
  },
  computed: {
    getSupplierName() {
      return function(value) {
        let supplierObj = this.supplierTypeData.find(supplier=>supplier.value == value);
        return supplierObj.name;
      }
    },
  },
  created() {
    // console.log(process.env);
    this.getList();
  },
  methods: {
    /** 查询供应商信息列表 */
    async getList() {
      this.loading = true;

      let res = await listSupplier(this.queryParams);
      if(res.code == 200) {
        this.supplierList = res.rows;
        this.total = res.total;
        this.loading = false;
      }
    },

    // 页数变化时
    pagination(data) {
      if(!data) return;

      this.queryParams.pageSize = data?.pageSize || 10;
      this.queryParams.pageNum  = data?.pageNum || 1;
      this.getList();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supplierId: null,
        supplierName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids      = selection.map((item) => item.supplierId);
      this.single   = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const supplierId = row.supplierId || this.ids;

      getSupplier(supplierId).then((response) => {
        this.form = response.data || this.form;
        this.open = true;
        this.title = "修改供应商信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.supplierId != null) {
            updateSupplier(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplier(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const supplierIds = row.supplierId || this.ids;
      this.$modal
        .confirm('是否确认删除供应商信息编号为"' + supplierIds + '"的数据项？')
        .then(function () {
          return delSupplier(supplierIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pm/supplier/export",
        {
          ...this.queryParams,
        },
        `supplier_${new Date().getTime()}.xlsx`
      );
    },

    // 配置请求方法
    handleAddRequestFunc(row) {
      // console.log(row);
      this.supplierId = row.supplierId || '';
      this.requestMethod.open = true;
    }
  },
};
</script>

<style lang="scss" scoped>

</style>
